package com.example.user.v1;

import com.example.model.common.dtos.ResponseResult;
import com.example.user.service.AclMenuService;
import com.example.user.service.AclUserService;
import com.example.utils.common.AppJwtUtil;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/backend/v1/user")
public class AclUserController {

    @Autowired
    AclUserService aclUserService;

    @Autowired
    AclMenuService aclMenuService;

    @GetMapping("/userInfo")
    @PreAuthorize("hasAuthority('admin')")
    public ResponseResult getUserInfoById(@RequestParam String id){
        return ResponseResult.okResult(aclUserService.getById(id));
    }

    @GetMapping("/getMenuList")
    public ResponseResult getMenuList( HttpServletRequest request){
        String token = request.getHeader("token");
        Jws<Claims> jws = AppJwtUtil.getJws(token);
        Claims claims = jws.getBody();
        String userId = claims.get("id").toString();
        log.info("登录用户id为: {}",userId);
        return ResponseResult.okResult(aclMenuService.getMenuListByUserId(userId));
    }
}
